<!DOCTYPE html>
<html lang="en"><head>
    <title>CZH-DEV BLOG</title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="format-detection" content="telephone=no" />
    <meta name="theme-color" content="#000084" />
    <link rel="icon" href="https://czh-dev.gitee.io/czh-blog.gitee.io//favicon.ico">
    <link rel="canonical" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">
    
    
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"></button>
            <a class="brand" href="https://czh-dev.gitee.io/czh-blog.gitee.io/">CZH-DEV BLOG</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/about/">
                                    
                                    <span>About</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/post/">
                                    
                                    <span>All posts</span>
                                </a>
                            </li>
                        
                    
                        
                            <li>
                                <a href="/czh-blog.gitee.io/ebook/">
                                    
                                    <span>Resource</span>
                                </a>
                            </li>
                        
                    
                </ul>
            </div>
        </div>
    </div>
</nav><div id="content" class="container">
<div style="display: flex;">
  <div class="row-fluid navmargin">
    <div class="page-header">
      <h1>Docker - Sat, Feb 11, 2023</h1>
    </div>
    <p class="lead"></p>
    <p>Docker</p>
<h2 id="虚拟化概念">虚拟化概念</h2>
<h3 id="传统的虚拟化技术">传统的虚拟化技术</h3>
<p>虚拟化，是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机。在一台计算机上同时运行多个逻辑计算机，每个逻辑计算机可运行不同的操作系统，并且应用程序都可以在相互独立的空间内运行而互不影响，从而显著提高计算机的工作效率。</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302111240460.png" alt="图片1"></p>
<p>​	随着硬件厂商的不断发展,很多在虚拟机里面的指令不需要通过虚拟的硬件层走到真正的硬件层.硬件厂商是支持直接在虚拟机里面实用指令操作硬件,这个技术我们就称为是硬件辅助的虚拟化.这种硬件的辅助虚拟化比起软件虚拟的硬件层来说,他不需要模拟所有的硬件.有些指令是直接运行在虚拟机上来操作硬件的.性能和效率比传统的虚拟化高更高些.</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302111240303.png" alt="图片2"></p>
<h3 id="系统级别的虚拟化">系统级别的虚拟化</h3>
<p>特点: 1 不需要模拟硬件层  2共享同一个宿主机的内核</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302111240843.png" alt="图片3"></p>
<h3 id="传统虚拟化和容器虚拟化的区别">传统虚拟化和容器虚拟化的区别</h3>
<p>​			传统虚拟化			                   容器虚拟化</p>
<p>创建速度		很慢			                            非常快</p>
<p>性能消耗	        通过对于硬件层模拟,增加了</p>
<p>​			系统调用链的环节,有性能损耗	    共享内核,几乎没有性能消耗</p>
<p>资源消耗		很大			                            很小,一台机器可以轻松创建多个容器</p>
<p>操作系统覆盖	 支持Linux,Window,Mac等	           仅仅是内核所支持的OS</p>
<h2 id="初识docker">初识Docker</h2>
<p><strong>Docker是什么</strong></p>
<p>​	Docker是一个用来装应用的容器，就好比杯子用来装水，笔筒用来装笔，书包用来装书一样，你可以把hello world放到Docker里，你可以把网站放到Docker里，你可以把你想的到的任何程序放到Docker里。</p>
<p><strong>Docker的思想</strong></p>
<pre><code>Docker 的思想来源于集装箱，集装箱最大的作用在于能够将货品标准化，在运输的过程中，不需要直接装载和卸载各种各样的商品，而是统一标准的集装箱，这样装载和卸载的环节就可以实现流水线和自动化了，并且商品封装在集装箱中，没有暴露在外面，提高安全性，集装箱与集装箱之间也是互相隔离，不会有任何相互影响的问题。我们也不再需要专门的船去运输专门的货品，只要货品在集装箱内好好封存着，那就可以使用一艘大船将他们都运走。并且你可以很方便的将集装箱装载到飞机、轮船、货车、火车，大大的提高了运输的便利性。
</code></pre>
<p>​    对于我们来说，Docker 也担任了集装箱类似的角色，我们的应用或者所依赖的第三方服务可以理解为货品，而 Docker 则是将我们的货品规范化管理起来，从而可以轻易的完成产品交付，环境转移等等诸多问题。还有我们的货品由Docker这个集中箱封装，对外隔离，提高安全性。并且 Docker 对于主机资源的消耗非常小，可以实现对服务器资源的最大利用。</p>
<p><strong>Docker 解决了哪些问题？</strong></p>
<pre tabindex="0"><code>1. 组织有序性
    以传统的方式部署你的应用，可能出现由于多个应用程序的依赖不用，导致应用之间的依赖变得非常混乱，若此时出现两个应用依赖同一个程序但却依赖不同版本，便会出现让人比较头痛的问题，使用Docker便可以很方便的帮我们管理各个应用的依赖，并以容器为基础将应用的运行环境独立开来，从而不会互相影响，还能提高资源利用率。

2. 便携性
    另一问题是，以传统的方式部署你的应用，如果我们的应用如果后期需要迁移（这里的迁移包括相同操作系统和不同操作系统的迁移），还是需要做大规模的集群，都是一件工作量非常大的操作。而容器化了的应用，对这些操作都是非常方便的。

3. 安全性
    最后一个不得不提的问题就是安全，以传统的方式部署你的应用，如果该应用被攻击者攻击，或者因为程序自身的bug，又或是运行环境多个应用间混乱的依赖关系导致的问题，这样的话，你的服务器系统安全危险级别会很高，因为这些问题将直接影响你的系统安全。但如果你的应用是运行在Docker容器中，那么一切的危害都会被隔离在容器内，因为容器的资源跟系统是隔离的，危害将被降到最低。
</code></pre><p><strong>Docker VS VM</strong></p>
<p>​	Docker 的核心是 基于 LXC（Liunx container） 技术的实现，可以理解为一个轻量级的虚拟机（实际上是容器的概念，Docker 不是虚拟机），Docker 使用 Linux 的 namespace 和 cgroups 技术实现容器隔离与资源限制。</p>
<p>类别                				Docker                       		VM
操作系统        				与宿主机共享 OS       		 在宿主机 OS 运行虚拟机 OS
部署难度        				非常简单                     		 组件多, 部署复杂
启动速度        				秒级                             		分钟级
执行性能        				和物理系统几乎一致   		VM 会占用一些资源
镜像体积       				 镜像是 MB 级别           		虚拟机镜像是 GB 级别
管理效率        				管理简单                      		组件相互依赖, 管理复杂
隔离性            				比较弱                         		彻底</p>
<p><strong>Docker 的应用场景</strong></p>
<pre tabindex="0"><code>面向产品：
    产品的交付模式改变
面向开发：
    个人开发环境发生改变
面向运维：
    获得比虚拟机性能损失更小的解决方案
    可以更快的进行自动化扩容（VM 是分钟级扩容，Docker 是秒级）
    Docker 适用于微服务架构
面向测试：
    同一环境下多版本测试

自动化、微服务、大规模集群（微博红包案例）百度的 Paas 平台、等等...
应用场景扩展：http://www.docker.org.cn/docker/151.html
docker中文学习网:http://www.docker.org.cn
</code></pre><h2 id="docker的基本概念">Docker的基本概念</h2>
<p>Docker 的基本概念：
​    Docker 有三大重要的概念：仓库、镜像、容器</p>
<h3 id="仓库repository">仓库（Repository)</h3>
<p>仓库是用于存放镜像的地方，类似于 Maven 仓库、Git 仓库等用途，仓库之上还有仓库注册服务器（Registry）的概念，上面存放着多个仓库，每个仓库中又有多个镜像，而每个镜像又可以有多个标签（tag）。目前最大的公开仓库是 Docker Hub，我们可以使用类似 Git 命令的方式去 pull/push 镜像</p>
<h3 id="镜像image">镜像（Image)</h3>
<p>可以理解为一个只读的模板，镜像可以用来创建容器，一个镜像可以创建多个容器，你可以直接从官方仓库（docker hub）或者其他私服仓库拉取（pull）想要的镜像，或通过 Dockerfile 来构建属于你自己的镜像，这一切操作都非常的简单</p>
<h3 id="容器container">容器（Container)</h3>
<p>容器是由镜像创建的实例，他可以被启动、停止、运行、删除。每个容器之间是互相隔离的、保证安全的平台。可以把容器看做为一个简单的 Linux 环境</p>
<h2 id="安装docker">安装Docker</h2>
<h3 id="centos">CentOS</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>Docker 的安装相对来说也算比较简单，咱们使用 CentOS7 来安装 Docker，不过 Docker 官方要求 CentOS 操作系统内核必须在 3.10 以上，所以我们这边使用 CentOS7 来安装；
</span></span><span style="display:flex;"><span>PS：可以使用 uname -r 来查看当前系统的内核版本
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>基于 CentOS7 的安装步骤如下:
</span></span><span style="display:flex;"><span>    1. 更新 yum 仓库，确保 yum 包都是最新版本
</span></span><span style="display:flex;"><span>        yum update
</span></span><span style="display:flex;"><span>    2. 查看是否有安装 docker，若有则将其删除
</span></span><span style="display:flex;"><span>        yum list installed|grep docker
</span></span><span style="display:flex;"><span>        yum remove docker...... <span style="color:#75715e"># 如果有的话则删除对应的包</span>
</span></span><span style="display:flex;"><span>    3. 安装需要的软件包，yum-util 提供 yum-config-manager 功能，另外两个是 devicemapper 驱动依赖的
</span></span><span style="display:flex;"><span>        yum install -y yum-utils device-mapper-persistent-data lvm2
</span></span><span style="display:flex;"><span>    4. 设置 yum 源
</span></span><span style="display:flex;"><span>        yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
</span></span><span style="display:flex;"><span>    5. 查看仓库中所有 docker 版本，选择你需要的版本安装
</span></span><span style="display:flex;"><span>        yum list docker-ce --showduplicates | sort -r
</span></span><span style="display:flex;"><span>    6. 安装 docker
</span></span><span style="display:flex;"><span>        yum install -y docker-ce
</span></span><span style="display:flex;"><span>    7. 设置 docker 开机自动启动
</span></span><span style="display:flex;"><span>        systemctl start docker <span style="color:#75715e"># 启动 docker 服务端</span>
</span></span><span style="display:flex;"><span>        systemctl enable docker <span style="color:#75715e"># 启用 docker 服务开机自启</span>
</span></span><span style="display:flex;"><span>    8. 校验 docker 是否启动成功
</span></span><span style="display:flex;"><span>        docker version
</span></span></code></pre></div><p>配置容器加速工具</p>
<p>访问阿里云:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>sudo mkdir -p /etc/docker
</span></span><span style="display:flex;"><span>sudo tee /etc/docker/daemon.json <span style="color:#e6db74">&lt;&lt;-&#39;EOF&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">{
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">  &#34;registry-mirrors&#34;: [&#34;https://rbw25ox1.mirror.aliyuncs.com&#34;]
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">}
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">EOF</span>
</span></span><span style="display:flex;"><span>sudo systemctl daemon-reload
</span></span><span style="display:flex;"><span>sudo systemctl restart docker
</span></span></code></pre></div><h3 id="windows">Windows</h3>
<ol>
<li>
<p>在<a href="https://hub.docker.com/?overlay=onboarding">Docker Hub Container Image Library | App Containerization</a>官网下载Docker Desktop</p>
</li>
<li>
<p>用户名：czhqwer     密码：cz18***</p>
</li>
<li>
<p>安装WSL（<a href="https://docs.microsoft.com/zh-cn/windows/wsl/install">官网</a>）：</p>
<ul>
<li>
<p>可以在PowerShell或Windows命令提示符中输入此命令，然后重启计算机安装运行适用于Linux的Windows子系统（WSL）所需的全部内容</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>wsl --install
</span></span></code></pre></div></li>
</ul>
</li>
</ol>
<h2 id="容器基本操作">容器基本操作</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>启动容器:
</span></span><span style="display:flex;"><span>	docker run IMAGE <span style="color:#f92672">[</span>COMMAND<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>ARGS...<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>	run 命令解释.
</span></span><span style="display:flex;"><span>	1. 命令会检测当前要运行的镜像在本地存不存在,若不存在他会自动 pull 下来再运行
</span></span><span style="display:flex;"><span>  2. 运行后会启动一个容器, 并且开启一个文件系统, 你可以直接在容器内创建文件夹. 运行一个指定的程序, 当程序退出后, 容器也就退出了
</span></span><span style="display:flex;"><span>  3. 运行时的容器无法删除, 只能停止后删除, 或者加上 -f 参数强制删除
</span></span><span style="display:flex;"><span>	示例: docker run centos echo <span style="color:#e6db74">&#39;hello docker&#39;</span>
</span></span><span style="display:flex;"><span>	     docker run --name 27-web-01 centos echo <span style="color:#e6db74">&#39;hello docker&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>启动守护式容器:
</span></span><span style="display:flex;"><span>	docker run -i -t IMAGE /bin/bash
</span></span><span style="display:flex;"><span>		-i --interactive<span style="color:#f92672">=</span>true|false 默认是false:允许你对容器内的标准输入 <span style="color:#f92672">(</span>STDIN<span style="color:#f92672">)</span> 进行交互
</span></span><span style="display:flex;"><span>		-t --tty<span style="color:#f92672">=</span>true|false 默认是false:在新容器内指定一个伪终端或终端
</span></span><span style="display:flex;"><span>	示例: docker run -i -t centos /bin/bash
</span></span><span style="display:flex;"><span>	使用exit退出守护式容器.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>查看容器:
</span></span><span style="display:flex;"><span>	docker ps <span style="color:#f92672">[</span>-a<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>-l<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>		-a:表示列举所有的容器
</span></span><span style="display:flex;"><span>		-l :表示列举最近创建的容器.	
</span></span><span style="display:flex;"><span>	默认情况容器在运行之后就停止了,ps只是显示正在运行的容器.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>查看容器详细信息:
</span></span><span style="display:flex;"><span>	docker inspect <span style="color:#f92672">[</span>容器名字|容器唯一标识<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>	要查看容器详细信息,需要输入很长串的字母.
</span></span><span style="display:flex;"><span>	docker允许用户自定义容器的名字:
</span></span><span style="display:flex;"><span>		docker run --name<span style="color:#f92672">=</span>自定义名 -i -t IMAGE /bin/bash
</span></span><span style="display:flex;"><span>	查看容器的ip地址
</span></span><span style="display:flex;"><span>		docker inspect 27-web-03 | grep IPAddress
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>重新启动停止的容器:
</span></span><span style="display:flex;"><span>	docker start <span style="color:#f92672">[</span>-i<span style="color:#f92672">]</span> 容器名
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>删除停止的容器:
</span></span><span style="display:flex;"><span>	docker rm 容器名	
</span></span><span style="display:flex;"><span>	只能删除停止的容器,但是不能删除正在运行中的容器.
</span></span><span style="display:flex;"><span>	docker rm <span style="color:#66d9ef">$(</span>docker ps -aq<span style="color:#66d9ef">)</span>
</span></span></code></pre></div><h3 id="守护式容器">守护式容器</h3>
<p>我们之前的学习,大多是容器都是在创建好之后就关闭了.在真实的案例中,我们需要长期运行的容器提供服务.这就是docker的守护式容器.
特点:
​          能长期运行
​          没有交互式会话
​          适合运行应用程序和服务.</p>
<pre tabindex="0"><code>以守护形式运行容器:
	启动容器:docker run -i -t IMAGE /bin/bash
	退出但不关闭容器:Ctrl+P Ctrl+Q

附加到运行中的容器:
    进入到原来的容器的进程中,执行的还是原来的命令
	docker attach 容器名 

启动守护式容器:
    1 进入容器中
    2 在容器的内部去启动命令
    3 退出容器的时候要以守护式启动

	docker run -d 镜像名 [COMMAND] [ARG...]
	示例:docker run --name dc1 -d centos /bin/sh -c &#34;while true; do echo helloworld;sleep 1;done&#34;

查看容器日志:
	docker logs [-f] [-t] [--tail] 容器名
		-f --follow=true|false 默认为false 一致跟踪日志的变化,并返回结果
		-t --timestamps=true|false 默认为false 在返回的结果上加上时间戳
		--tail = “all” 返回后几行的日志数据.

查看容器内进程:
	docker top 容器名

在运行中的容器内启动新进程:
	docker exec [-d] [-i] [-t] 容器名 [COMMAND] [ARG...]
	示例:docker exec -i -t dc1 /bin/bash

停止守护式容器:
	docker stop 容器名 :发送一个信号给容器,等待容器的关闭.
	docker kill 容器名 :直接停止容器.
</code></pre><h2 id="部署静态网站">部署静态网站</h2>
<h3 id="设置容器的端口映射">设置容器的端口映射:</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>run <span style="color:#f92672">[</span>-P<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>-p<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>-P , --publish-all<span style="color:#f92672">=</span>true|false 默认为false :将为容器所有暴露的端口进行映射.
</span></span><span style="display:flex;"><span>	示例:docker run -P -i -t centos /bin/bash
</span></span><span style="display:flex;"><span>-p, --publish<span style="color:#f92672">=[]</span> :给指定的端口进行映射.
</span></span><span style="display:flex;"><span>    方式一:containerPort
</span></span><span style="display:flex;"><span>    	示例:docker run -p <span style="color:#ae81ff">80</span> -i -t centos /bin/bash
</span></span><span style="display:flex;"><span>    方式二:hostPort:containerPort
</span></span><span style="display:flex;"><span>    	示例:docker run -p 8080:80 -i -t centos /bin/bash
</span></span></code></pre></div><h3 id="nginx部署流程">Nginx部署流程</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>Nginx部署流程
</span></span><span style="display:flex;"><span>	创建映射80端口的交互式容器
</span></span><span style="display:flex;"><span>	安装Nginx
</span></span><span style="display:flex;"><span>	安装文本编辑器vim
</span></span><span style="display:flex;"><span>	创建静态页面
</span></span><span style="display:flex;"><span>	修改Nginx配置文件
</span></span><span style="display:flex;"><span>	运行Nginx
</span></span><span style="display:flex;"><span>	验证网站访问
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>步骤:
</span></span><span style="display:flex;"><span>	1.创建一个守护式容器,并映射容器的80端口.
</span></span><span style="display:flex;"><span>	    docker run -p <span style="color:#ae81ff">80</span> --name<span style="color:#f92672">=</span>web -it centos /bin/bash
</span></span><span style="display:flex;"><span>	2.进入容器后,需要安装nginx和vim,执行如下命令:
</span></span><span style="display:flex;"><span>	    yum install -y epel-release
</span></span><span style="display:flex;"><span>	    yum install -y nginx
</span></span><span style="display:flex;"><span>	3.在容器内部,进入到/usr/share/nginx/html 目录,修改index.html文件
</span></span><span style="display:flex;"><span>	    vim index.html
</span></span><span style="display:flex;"><span>	4.运行nginx命令,启动服务 /usr/sbin/nginx
</span></span><span style="display:flex;"><span>	5.Ctrl+P和Ctrl+Q退出容器.
</span></span><span style="display:flex;"><span>	6.在宿主机上使用curl命令查看是否能访问.
</span></span><span style="display:flex;"><span>	    curl http://127.0.0.1:32769/index.html
</span></span></code></pre></div><p><strong>其他问题:</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>其他问题:
</span></span><span style="display:flex;"><span>	1<span style="color:#f92672">.</span><span style="color:#a6e22e">使用docker</span> inspect 容器名可以查看容器的ip地址<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span>	2<span style="color:#f92672">.</span><span style="color:#a6e22e">在宿主机中使用容器的ip也是能访问到该静态页面的</span><span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span>	3<span style="color:#f92672">.</span><span style="color:#a6e22e">使用docker</span> stop 容器名 关闭容器<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span>	4<span style="color:#f92672">.</span><span style="color:#a6e22e">使用docker</span> start 容器名 启动容器<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span>	5<span style="color:#f92672">.</span><span style="color:#a6e22e">使用docker</span> top 容器名<span style="color:#f92672">,</span>发现重新启动容器后<span style="color:#f92672">,</span>nginx服务并没有重新启动<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span>	6<span style="color:#f92672">.</span><span style="color:#a6e22e">我们可以使用docker</span> exec 容器名 nginx 来启动容器中的nginx服务<span style="color:#f92672">.</span> docker exec <span style="color:#f92672">-</span>it web1 <span style="color:#f92672">/</span>usr<span style="color:#f92672">/</span>sbin<span style="color:#f92672">/</span>nginx
</span></span><span style="display:flex;"><span>	7<span style="color:#f92672">.</span><span style="color:#a6e22e">我们发现使用原来的端口映射已经访问不到了</span><span style="color:#f92672">,</span>当重新启动容器会重新给我们进行端口的映射<span style="color:#f92672">.</span>
</span></span></code></pre></div><h2 id="docker的镜像和仓库">Docker的镜像和仓库</h2>
<p>Docker Image镜像:
​	容器的基石</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>列出镜像:
</span></span><span style="display:flex;"><span>	docker images <span style="color:#f92672">[</span>OPTIONS<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>REPOSITORY<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>	-a, --all<span style="color:#f92672">=</span>false :表示列举出所有的镜像.默认并不显示中间层的镜像.
</span></span><span style="display:flex;"><span>	-f, --filter<span style="color:#f92672">=[]</span> :表示添加过滤条件.
</span></span><span style="display:flex;"><span>	--no-trunc<span style="color:#f92672">=</span>false:表示对信息进行截断.默认情况是会截断镜像的唯一id的.
</span></span><span style="display:flex;"><span>	-q, --quiet<span style="color:#f92672">=</span>false:表示值显示镜像的唯一id
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>REPOSITORY 仓库: 是一切镜像的集合.
</span></span><span style="display:flex;"><span>REGISTRY 注册仓库:包含REPOSITORY 的仓库.
</span></span><span style="display:flex;"><span>TAG 标签:我们之前运行的centos默认使用的是lastest的标签.仓库名+镜像名就能唯一确定一个镜像.
</span></span><span style="display:flex;"><span>如果使用docker images -a 看到没有仓库名和标签名的其实是中间层镜像.
</span></span><span style="display:flex;"><span>查看镜像:
</span></span><span style="display:flex;"><span>	docker inspect <span style="color:#f92672">[</span>OPTIONS<span style="color:#f92672">]</span> CONTAINER|IMAGE <span style="color:#f92672">[</span>CONTAINER|IMAGE...<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>	示例:docker inspect 49f796
</span></span><span style="display:flex;"><span>删除镜像:
</span></span><span style="display:flex;"><span>	docker rmi <span style="color:#f92672">[</span>OPTIONS<span style="color:#f92672">]</span> IMAGE <span style="color:#f92672">[</span>IMAGE...<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>	-f, --force<span style="color:#f92672">=</span>false :表示强制删除镜像.
</span></span><span style="display:flex;"><span>	--no-proune<span style="color:#f92672">=</span>false :会保留未被打标签的父镜像.
</span></span><span style="display:flex;"><span>	删除对应仓库所有的镜像:docker rmi  <span style="color:#66d9ef">$(</span>docker images -q centos<span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>	
</span></span></code></pre></div><h3 id="获取和推送镜像">获取和推送镜像</h3>
<pre tabindex="0"><code>Docker中REGISTER仓库有些类似Maven的中央仓库.我们通过类似Git中的pull和push命令从REGISTER仓库中拉取镜像和推送镜像.
查找镜像:
	方式一:
	              Docker Hub    https://hub.docker.com/
	方式二:
	              docker search [OPTIONS] TERM
                                          --no-trunc=false : 截断操作
                                          -s,--starts=0 : 每个镜像都会有star,我们可以通过-s操作来限定star数.
	             一次最多返回25条记录.
拉取镜像:
	docker pull [OPTIONS] NAME [:TAG]
	-a, --all-tags=false :会把所有仓库中标记的镜像下载到本地中
	示例:docker pull ubuntu:14.04
	我们会发现我们拉取镜像的速度非常慢.我们可以修改REGISTER仓库的地址来加快下载的速度.	
	sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json &lt;&lt;-&#39;EOF&#39;
    {
      &#34;registry-mirrors&#34;: [&#34;https://175i7vum.mirror.aliyuncs.com&#34;]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
	docker pull ubuntu:12.10
	我们会发现明显速度已经提升很多了.

推送镜像:
	docker push 镜像名.
	步骤:
		1.注册hub的账号.
		2.登陆hub的账号.docker login
		3.使用push命令推送到hub中
		    docker push fjl0720/commit_test1
</code></pre><p>补充操作过程</p>
<pre tabindex="0"><code>1\登录阿里云网站 
2\个人实例的固定密码
   个人实例中--&gt;访问凭证--&gt;设置密码
3\登录后,点击控制台,进入容器镜像服务 --&gt;实例列表 --&gt;个人实例--&gt;
4\新增命名空间
5\新增镜像仓库
6\点击 新建的仓库 --&gt; 管理
   找到推送脚本
   $ sudo docker login --username=linhaifengnih**** registry.cn-hangzhou.aliyuncs.com
    $ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/wolfcode-w/wolfcode-w:[镜像版本号]
    $ sudo docker push registry.cn-hangzhou.aliyuncs.com/wolfcode-w/wolfcode-w:[镜像版本号]
    注意: 这里面 linhaifengnih**** 改为用户名全称. 
         [ImageId] 这是要推送的镜像id
         [镜像版本号] 表示镜像版本号,可以自定义.如1.0
 7\执行三个脚本命令, 在仓库的镜像版本中查看上传的镜像.
 
 如果是拉取镜像 
 	docker pull registry.cn-hangzhou.aliyuncs.com/wolfcode-w/wolfcode-w:[镜像版本号]
 
 如果支付宝登录名是中文的处理方案:
    换一个方式登录就可以了. 如:淘宝,钉钉等.
 
</code></pre><h3 id="构建镜像">构建镜像</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>构建镜像可以让我们保存对容器的修改,并再次使用.提供了自定义镜像的能力,以软件的形式打包并方法服务及其运行环境.
</span></span><span style="display:flex;"><span>docker里面提供了两种方式来构建镜像:
</span></span><span style="display:flex;"><span>	方式一:docker commit 通过容器构建
</span></span><span style="display:flex;"><span>	方式二:docker build 通过Dockerfile文件构建.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>使用commit构建镜像:
</span></span><span style="display:flex;"><span>	docker commit <span style="color:#f92672">[</span>OPTIONS<span style="color:#f92672">]</span> CONTAINER <span style="color:#f92672">[</span>REPOSITORY<span style="color:#f92672">[</span>:TAG<span style="color:#f92672">]]</span>
</span></span><span style="display:flex;"><span>	-a, --author<span style="color:#f92672">=</span>”” 指定镜像的作者.
</span></span><span style="display:flex;"><span>	-m, --message<span style="color:#f92672">=</span>”” 提交信息,构建信息
</span></span><span style="display:flex;"><span>	-p, --pause<span style="color:#f92672">=</span>true 默认情况在commit的时候会暂停容器,使用这个参数可以在构建的时候不暂停容器.
</span></span><span style="display:flex;"><span>	步骤:
</span></span><span style="display:flex;"><span>		1.创建一个容器并安装nginx服务
</span></span><span style="display:flex;"><span>		docker run -it --name commit_test -p <span style="color:#ae81ff">80</span> centos /bin/bash
</span></span><span style="display:flex;"><span>		2.在容器内部安装nginx,和之前步骤一样
</span></span><span style="display:flex;"><span>		3.提交镜像.
</span></span><span style="display:flex;"><span>		docker commit -a <span style="color:#e6db74">&#39;fjl&#39;</span> -m <span style="color:#e6db74">&#39;nginx_build&#39;</span> web fjl/web
</span></span><span style="display:flex;"><span>		4.通过docker images命令可以看到我们刚构建的镜像.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>		5.接下来我们就可以使用自己定义的镜像来启动容器.
</span></span><span style="display:flex;"><span>		docker run -d --name<span style="color:#f92672">=</span>nginx_web1 -p <span style="color:#ae81ff">80</span> fjl/web /usr/sbin/nginx -g <span style="color:#e6db74">&#34;daemon off;&#34;</span>
</span></span></code></pre></div><h3 id="使用dockerfile构建镜像">使用DockerFile构建镜像</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-dockerfile" data-lang="dockerfile"><span style="display:flex;"><span>使用Dockerfile构建镜像:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	什么是Dockerfile文件呢?其实就是包含了一些执行命令的文本文件.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>docker build <span style="color:#f92672">[</span>OPTIONS<span style="color:#f92672">]</span> PATH|URL|-<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    --force-rm<span style="color:#f92672">=</span>false<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    --no-cache<span style="color:#f92672">=</span>false<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    --pull<span style="color:#f92672">=</span>false<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    -q, --quiet<span style="color:#f92672">=</span>false<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    -rm<span style="color:#f92672">=</span>true<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    -t, --tag<span style="color:#f92672">=</span>””<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>步骤:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	1.在宿主机中,我们创建存放Dockerfile文件的目录和Dockerfile文件.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    mkdir -p dockerfile/df_test1<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    cd dockerfile/df_test1/<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    vi Dockerfile<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	2.编写Dockfile文件<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    <span style="color:#75715e"># First docker file for test</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    FROM centos<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    MAINTAINER fjl0720 <span style="color:#e6db74">&#34;fanjialong@wolfcode.cn&#34;</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    RUN sed -i <span style="color:#e6db74">&#39;s/mirrorlist/#mirrorlist/g&#39;</span> /etc/yum.repos.d/CentOS-*<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>        RUN sed -i <span style="color:#e6db74">&#39;s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g&#39;</span> /etc/yum.repos.d/CentOS-*<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    RUN yum install -y epel-release<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    RUN yum install -y nginx<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    EXPOSE <span style="color:#ae81ff">80</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	   <span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	3.使用build命令构建镜像.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    docker build -t<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;镜像名称&#39;</span> Dockerfile 所在的目录<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    例如:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    docker build -t<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;df/web1&#39;</span> /root/dockerfile/df1<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	4.通过我们构建好的镜像来创建容器.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	    docker run -d --name nginx_web3 -p <span style="color:#ae81ff">80</span> df/web1 /usr/sbin/nginx -g <span style="color:#e6db74">&#34;daemon off;&#34;</span><span style="color:#960050;background-color:#1e0010">
</span></span></span></code></pre></div><h3 id="dockerfile的指令">DockerFile的指令</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-dockerfile" data-lang="dockerfile"><span style="display:flex;"><span>指令主要分为两种:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	注释 : <span style="color:#75715e"># Comment</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	指令 : INSTRUCTION argument<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">FROM</span><span style="color:#e6db74">:</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	包含两种格式:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	1. FROM  &lt;image&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	2. FROM  &lt;image&gt;:&lt;tag&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	image要求是已经存在的镜像,我们也称为基础镜像.必须是第一条非注释指令<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">MAINTAINER</span><span style="color:#e6db74">:</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	指定镜像的作者信息,包含镜像的所有者和联系信息.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">RUN</span>:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	指定当前镜像中运行的命令<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	RUN  &lt;command&gt; <span style="color:#f92672">(</span>shell模式<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	        /bin/sh -c command<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	        RUN echo hello<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	RUN <span style="color:#f92672">[</span>“executable”,”param1”,”param2”<span style="color:#f92672">]</span> <span style="color:#f92672">(</span>exec模式<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>                             RUN <span style="color:#f92672">[</span>“/bin/bash”,”-c”,”echo hello”<span style="color:#f92672">]</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>                             <span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	每个RUN命令都会在当前镜像的上层创建一个新的镜像来运行指令.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">EXPOSE</span><span style="color:#e6db74">:</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	指定运行该镜像的容器使用的端口.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	虽然我们在构建镜像的时候暴露了端口号,但是我们在运行容器的时候依然需要指定端口的映射.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	我们使用EXPOSE只是告诉Docker运行该镜像的容器会使用80端口,出于安全的考虑,docker并不会打开该端口.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	而是需要我们在使用该镜像运行容器的时候指定端口的映射.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">CMD</span>:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	CMD指令提供容器默认运行的命令,和之前讲的RUN指令类似.都是执行一个命令,但是RUN命令指定的命令是在镜像构建的过程运行的.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	CMD的命令是在容器运行的时候运行的.如果我们在docker run命令中指定运行的命令的时候,CMD的指令会被覆盖,默认命令就不会执行.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	CMD命令是指定容器启动的时候默认命令.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	两种模式.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	   CMD <span style="color:#f92672">[</span>“executable”,”param1”,”param2”<span style="color:#f92672">]</span> <span style="color:#f92672">(</span>exec模式<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	   CMD command param1 param2 <span style="color:#f92672">(</span>shell 模式<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span> 	   CMD <span style="color:#f92672">[</span>”param1”,”param2”<span style="color:#f92672">]</span> <span style="color:#f92672">(</span>作为ENTRYPOINT指令的默认参数.<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	通过构建的镜像来创建容器<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker run -p <span style="color:#ae81ff">80</span> --name cmd_test1 -itd fjl0720/df_test3<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker top cmd_test1<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	发现已经启动nginx了.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	如果我们在启动的时候指定了参数,默认的CMD命令就会被覆盖了.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">ENTRYPOINT</span>:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	这个和我们刚刚讲的CMD指令非常相似,唯一的区别:不会给docker run的启动命令给覆盖.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	如果需要覆盖ENTRYPOINT的指令,需要在docker run使用docker run --entrypoint覆盖.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ENTRYPOINT <span style="color:#f92672">[</span>“executable”,”param1”,”param2”<span style="color:#f92672">]</span> <span style="color:#f92672">(</span>exec模式<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ENTRYPOINT command param1 param2 <span style="color:#f92672">(</span>shell 模式<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker build -t<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;fjl0720/df_test4&#34;</span> .<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker run -p <span style="color:#ae81ff">80</span> --name ep_test1 -d lanxw0720/df_test4 <span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker ps -l<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	可以发现,启动的容器依然使用的ENTRYPOINT指定的命令执行.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>在宿主机中创建一个index.html文件 , 在构建镜像把我们宿主机的index.html文件把默认的首页文件替换掉<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">ADD</span>:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>    <span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	将文件和目录复制到使用dockerfile构建的镜像中.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	目标的来源可以本地的地址也可以是远程地址.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	如果是本地地址,本地地址必须是构建目录中的相对地址<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	对于远程URL,docker并不推荐使用,更建议使用的是curl或者wget的命令来获取<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	目标路径需要指定镜像中的绝对路径<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ADD &lt;src&gt;...&lt;dest&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ADD <span style="color:#f92672">[</span>“&lt;src&gt;”...”&lt;dest&gt;”<span style="color:#f92672">](</span>适用于文件路径有空格的情况<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">COPY</span>:	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	同上.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	COPY&lt;src&gt;...&lt;dest&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	COPY<span style="color:#f92672">[</span>“&lt;src&gt;”...”&lt;dest&gt;”<span style="color:#f92672">](</span>适用于文件路径有空格的情况<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span> 	在Dockerfile所在目录添加index.html文件<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker build -t<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;lanxw0720/df_test6&#34;</span> .<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker run -p <span style="color:#ae81ff">80</span> --name add_test1 -d lanxw0720/df_test6<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	curl http://127.0.0.1:32775<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">VOLUME</span><span style="color:#e6db74">:</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	用于基于镜像创建的容器添加卷,一个卷可以存在一个或者多个容器的特定目录.这个目录可以绕过联合文件系统.提供共享数据和持久化数据的	功能.<span style="color:#f92672">(</span>后面单独讲<span style="color:#f92672">)</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">WORKDIR</span><span style="color:#e6db74">:</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	WORKDIR /path/to/workdir<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	这个指令从指令创建一个容器是,在容器内部设置工作目录.ENTRYPOINT和CMD的命令都会在这个目录下执行.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	我们也可以使用这个命令给后续的构建中指定工作目录.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	通常会使用绝对路径,如果使用了相对路径,那这个路径会一致传递下去.如下所示:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	WORKDIR /a<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	WORKDIR b<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	WORKDIR c<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	RUN pwd<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	结果<span style="color:#f92672">====</span>&gt;/a/b/c<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">ENV</span>:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ENV &lt;key&gt;&lt;value&gt;<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ENV &lt;key&gt;&lt;value&gt;...<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	这个指令主要是来设置环境变量,这个环境变量在构建过程中和运行过程中都有效.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">USER</span><span style="color:#e6db74">:</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	USER daemon<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	指定镜像会以什么样的用户去运行.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	比如:USER nginx<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	基于该镜像启动的容器就会以nginx的用户来运行.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	如果没有指定USER,容器会使用root用户来运行.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#66d9ef">ONBUILD</span>:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	ONBUILD <span style="color:#f92672">[</span>INSTRUCTION<span style="color:#f92672">]</span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	镜像触发器.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	当一个镜像被其他镜像作为基础镜像时执行<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	会在构建过程中插入指令<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	示例:<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker run -p <span style="color:#ae81ff">80</span> --name onbuild_test1 -d lanxw0720/df_test7<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	curl http://127.0.0.1:32776<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	发现在构建这个镜像的时候并没有执行COPY命令.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	接下来我们基于这个镜像来构建新的镜像.<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker build -t<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;lanxw0720/df_test8&#34;</span> .<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	docker run -p <span style="color:#ae81ff">80</span> --name onbuild_test2 -d lanxw0720/df_test8<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	curl http://127.0.0.1:32777<span style="color:#960050;background-color:#1e0010">
</span></span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010"></span>	此时发现已经执行COPY命令了.<span style="color:#960050;background-color:#1e0010">
</span></span></span></code></pre></div><h2 id="容器的数据管理">容器的数据管理</h2>
<p>什么是数据卷(Data Volume):
​	我们都知道Docker其中一个理念就是应用和环境打包.通常docker容器的生命周期都是和容器中的应用是一致的.而我们对数据的要求是持久化,另外docker容器之间也需要有一个共享数据的渠道.这些需求就催生了数据卷的需求.
​	数据卷是经过特殊设计的目录,可以绕过联合文件系统(UFS),为一个或者多个容器提供访问.
​	数据卷设计的目的,在于数据的永久化,它完全独立与容器的生命周期,因此Docker不会在容器删除的时候删除其关联的数据卷,也不会存在类似垃圾收集机制,对容器应用的数据卷进行处理.</p>
<p><img src="https://czh-pic.oss-cn-guangzhou.aliyuncs.com/202302111241757.png" alt="图片4"></p>
<p>通过上面这张图
​       1.数据卷是独立于容器的存在,他存在于宿主机中.和容器是分离的.
​       2.docker数据卷可以是文件或者目录.
​       3.docker容器可以使用数据卷和宿主机进行数据的共享.
​       4.同一个文件或者目录支持多个容器的访问.实现了容器间的数据共享和交换.
数据卷的特点:
​       数据卷在容器启动时初始化,如果容器使用的镜像在挂载点包含了数据,这些数据会拷贝到新初始化的数据卷中.
​       数据卷可以容器之间共享和重用.
​       可以对数据卷里的内容直接进行修改.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>为容器添加数据卷:
</span></span><span style="display:flex;"><span>                       宿主机路径      容器内路径
</span></span><span style="display:flex;"><span>       docker run -v ~/container_data:/data -it centos /bin/bash
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>为数据卷添加访问权限
</span></span><span style="display:flex;"><span>       docker run -v  ~/datavolume:/data:ro -it centos /bin/bash
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>使用Dockerfile构建包含数据卷的镜像
</span></span><span style="display:flex;"><span>Dockerfile指令:VOLUME<span style="color:#f92672">[</span>“/data”<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>示例:
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Test</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>FROM centos
</span></span><span style="display:flex;"><span>VOLUME <span style="color:#f92672">[</span><span style="color:#e6db74">&#34;/datavolume1&#34;</span><span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>CMD /bin/bash
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>执行构建命令:docker build -t lanxw0720/dvt .
</span></span><span style="display:flex;"><span>我们在容器启动的时候,我们在容器中指定的数据卷在容器中都会进行一次完整的初始化.根据镜像来创建的容器.使用的数据卷就没办法共享.当我们不能访问到本地目录时,我们怎么共享数据呢?
</span></span></code></pre></div><h2 id="多容器管理docker-compose">多容器管理（docker-compose）</h2>
<pre tabindex="0"><code>多容器管理（docker-compose）：
    前面讲了 Dockerfile 用来构建 Docker 镜像，那么 docker-compose 则是用来创建容器的。 Docker 有三个主要的功能：Build、Ship 和 Run，使用 docker-compose 可以帮我们在 Run 的层面解决很多实际问题。docker-compose 通过一个 yaml 模板文件来统一管理多个容器的配置，如网络、数据卷、执行指令、环境变量、资源限制等等。有了 docker-compose 我们便可以一键重启、关闭、删除、监控所有的 docker 服务，只需要一次配置，则可以对容器进行统一管理，那么此时我们则不必为了每次要运行一堆容器时写大量的命令而头疼。

    1. 安装 docker-compose：
    
    sudo curl -L &#34;http://mirrors.aliyun.com/docker-toolbox/linux/compose/1.9.0/docker-compose-$(uname -s)-$(uname -m)&#34; -o /usr/local/bin/docker-compose
    
    要安装其他版本的 Compose，请替换 1.24.1。

	将可执行权限应用于二进制文件：
	sudo chmod +x /usr/local/bin/docker-compose
	创建软链：
	sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
	测试是否安装成功：
    docker-compose --version

    2. 配置 docker-compose.yml 文件
        version: &#39;2&#39; # docker 的版本
        services: # 配置的容器列表
            CONTAINER_NAME: # 容器的名称
                image: BASE_IMAGE # 这个一个容器的基础镜像
                ports: # 你的容器需不需要做端口映射
                    - &#34;host_port:container_port&#34;
                volumes: # 数据卷配置
                    - host_dir:container_dir
                environment: # 环境变量（map 的配置方式 key: value）
                    PARAM: VALUE
                environments: # 环境变量（数组的配置方式 - key=value）
                    - PARAM=VALUE
                restart: always # 容器的重启策略
                dns: # dns 的配置
                    - &#34;8.8.8.8&#34;
    ------------------------------------------------------------------
                restart 容器的重启策略：
                    no：默认策略，在容器退出时不重启容器
                    on-failure：在容器非正常退出时（退出状态非0），才会重启容器
                    on-failure:3：在容器非正常退出时重启容器，最多重启3次
                    always：在容器退出时总是重启容器
                    unless-stopped：在容器退出时总是重启容器，但是不考虑在Docker守护进程启动时就已经停止了的容器

    3. 常用命令
        docker-compose up -d # 启动所有服务并在后台运行
        docker-compose ps # 查看服务运行状态
        docker-compose restart # 重启所有服务
        docker-compose restart myApp # 重启 myApp 服务
        docker-compose start # 开启所有服务
        docker-compose start myApp # 开启 myApp 服务
        docker-compose stop # 停止所有服务
        docker-compose stop myApp # 停止 myApp 服务
        docker-compose rm # 删除所有服务
        docker-compose rm myApp # 删除 myApp fuwu
</code></pre><h2 id="总结常用命令">总结常用命令</h2>
<ul>
<li>
<p>查看镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker images
</span></span></code></pre></div></li>
<li>
<p>查看容器</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker ps 
</span></span></code></pre></div><blockquote>
<p><strong>-a</strong>：查看所有容器（包括退出的）</p>
<p><strong>-l</strong>：查看最后一次运行的容器</p>
<p><strong>-f</strong> status=exited：查看体制的容器</p>
</blockquote>
</li>
<li>
<p>搜索镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker search <span style="color:#f92672">[</span>镜像名称<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>eg:
</span></span><span style="display:flex;"><span>$ docker search centos
</span></span></code></pre></div></li>
<li>
<p>拉取镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker pull <span style="color:#f92672">[</span>镜像名称<span style="color:#f92672">]</span>:<span style="color:#f92672">[</span>版本号--不填表示拉取最新<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>eg:
</span></span><span style="display:flex;"><span>$ docker pull centos:centos7.9.2009
</span></span></code></pre></div></li>
<li>
<p>删除镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker rmi <span style="color:#f92672">[</span>镜像ID<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span>eg:删除所有镜像
</span></span><span style="display:flex;"><span>$ docker rmi <span style="color:#66d9ef">$(</span>docker images -q<span style="color:#66d9ef">)</span>
</span></span></code></pre></div></li>
<li>
<p>创建守护容器</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker run -it --name<span style="color:#f92672">=[</span>容器名称<span style="color:#f92672">]</span> <span style="color:#f92672">[</span>镜像名称<span style="color:#f92672">]</span>:<span style="color:#f92672">[</span>标签<span style="color:#f92672">]</span> /bin/bash
</span></span><span style="display:flex;"><span>eg:
</span></span><span style="display:flex;"><span>$ docker run -it eeb6ee3f44bd /bin/bash
</span></span></code></pre></div><blockquote>
<p>退出守护容器但不关闭 <code>CTRL+P+Q</code></p>
</blockquote>
</li>
<li>
<p>登录守护容器</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker exec -it <span style="color:#f92672">[</span>容器名称<span style="color:#f92672">](</span>或者容器ID<span style="color:#f92672">)</span> /bin/bash
</span></span><span style="display:flex;"><span>eg:
</span></span><span style="display:flex;"><span>$ docker exec -it 0a853a7152a4 /bin/bash
</span></span></code></pre></div></li>
<li>
<p>停止和启动容器</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ docker stop <span style="color:#f92672">[</span>容器名称<span style="color:#f92672">](</span>或者容器ID<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span>$ docker start <span style="color:#f92672">[</span>容器名称<span style="color:#f92672">](</span>或者容器ID<span style="color:#f92672">)</span>
</span></span></code></pre></div></li>
<li></li>
</ul>

    <h4><a href="https://czh-dev.gitee.io/czh-blog.gitee.io/">Back to Home</a></h4>
  </div>

  <div class="span3 bs-docs-sidebar" style="position:fixed;right: 40px;top: 50px;">
    <h1>catalogue</h1>
    <ul class="nav nav-list bs-docs-sidenav">
      <div class="toc-div">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#虚拟化概念">虚拟化概念</a>
      <ul>
        <li><a href="#传统的虚拟化技术">传统的虚拟化技术</a></li>
        <li><a href="#系统级别的虚拟化">系统级别的虚拟化</a></li>
        <li><a href="#传统虚拟化和容器虚拟化的区别">传统虚拟化和容器虚拟化的区别</a></li>
      </ul>
    </li>
    <li><a href="#初识docker">初识Docker</a></li>
    <li><a href="#docker的基本概念">Docker的基本概念</a>
      <ul>
        <li><a href="#仓库repository">仓库（Repository)</a></li>
        <li><a href="#镜像image">镜像（Image)</a></li>
        <li><a href="#容器container">容器（Container)</a></li>
      </ul>
    </li>
    <li><a href="#安装docker">安装Docker</a>
      <ul>
        <li><a href="#centos">CentOS</a></li>
        <li><a href="#windows">Windows</a></li>
      </ul>
    </li>
    <li><a href="#容器基本操作">容器基本操作</a>
      <ul>
        <li><a href="#守护式容器">守护式容器</a></li>
      </ul>
    </li>
    <li><a href="#部署静态网站">部署静态网站</a>
      <ul>
        <li><a href="#设置容器的端口映射">设置容器的端口映射:</a></li>
        <li><a href="#nginx部署流程">Nginx部署流程</a></li>
      </ul>
    </li>
    <li><a href="#docker的镜像和仓库">Docker的镜像和仓库</a>
      <ul>
        <li><a href="#获取和推送镜像">获取和推送镜像</a></li>
        <li><a href="#构建镜像">构建镜像</a></li>
        <li><a href="#使用dockerfile构建镜像">使用DockerFile构建镜像</a></li>
        <li><a href="#dockerfile的指令">DockerFile的指令</a></li>
      </ul>
    </li>
    <li><a href="#容器的数据管理">容器的数据管理</a></li>
    <li><a href="#多容器管理docker-compose">多容器管理（docker-compose）</a></li>
    <li><a href="#总结常用命令">总结常用命令</a></li>
  </ul>
</nav>
      </div>
    </ul>
  </div>

</div>
<script src="https://cdn.jsdelivr.net/npm/gumshoejs@5.1.2/dist/gumshoe.min.js"></script>
<script>
  var spy = new Gumshoe('#TableOfContents a', {
    nested: true,
    nestedClass: 'active'
  });
</script>
<style>
   
  #TableOfContents li,
  #TableOfContents ul {
    list-style-type: none;
  }

  #TableOfContents ul {
    padding-left: 0px;
  }

  #TableOfContents li>a {
    display: block;
    padding: 4px 20px;
    font-size: 95%;
    color: #000000;
  }

  #TableOfContents li>a:hover,
  #TableOfContents li>a:focus {
    padding-left: 19px;
    color: #3A6bA5;
    text-decoration: none;
    background-color: transparent;
    border-left: 1px solid #3A6bA5;
  }

  #TableOfContents li.active>a,
  #TableOfContents li.active>a:hover,
  #TableOfContents li.active>a:focus {
    padding-left: 18px;
    font-weight: bold;
    color: #3A6bA5;
    background-color: transparent;
    border-left: 2px solid #3A6bA5;
  }

   
  #TableOfContents li>ul {
    padding-bottom: 10px;
  }

  #TableOfContents li li>a {
    padding-top: 1px;
    padding-bottom: 1px;
    padding-left: 30px;
    font-size: 14px;
    font-weight: normal;
  }

  #TableOfContents li li>a:hover,
  #TableOfContents li li>a:focus {
    padding-left: 29px;
  }

  #TableOfContents li li.active>a,
  #TableOfContents li li.active>a:hover,
  #TableOfContents li li.active>a:focus {
    padding-left: 28px;
    font-weight: 500;
  }

  #TableOfContents .nav-link.active+ul {
    display: block;
  }

  #TableOfContents li>ul {
    display: none;
  }

  #TableOfContents li.active>ul {
    display: inherit;
  }

  .toc-div {
    position: -webkit-sticky;
     
    position: sticky;
     
    top: 20px;
  }
</style>


        </div><footer class="container">
    <hr class="soften">
    <p>
    <a href="https://space.bilibili.com/1799809923">Love eating fried pork ribs</a> | 

&copy; 
<a href="http://jmf-portfolio.netlify.com" target="_blank">
    JM Fergeau
</a>
<span id="thisyear">2023</span>

    | My site


        | Built on <a href="//gohugo.io" target="_blank">Hugo</a>

</p>
    <p class="text-center">
        <a href="https://facebook.com">Facebook</a> 
        <a href="https://twitter.com">Twitter</a> 
        <a href="https://linkedin.com">Linkedin</a> 
        <a href="https://github.com">GitHub</a> 
        <a href="https://gitlab.com">GitLab</a>
    </p>
</footer>

</body><link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/bootstrap-responsive.css">
<link rel="stylesheet" href="/czh-blog.gitee.io/css/style.css">

<script src="/czh-blog.gitee.io/js/jquery.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-386.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-transition.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-alert.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-modal.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-dropdown.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-scrollspy.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tab.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-tooltip.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-popover.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-button.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-collapse.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-carousel.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-typeahead.js"></script>
<script src="/czh-blog.gitee.io/js/bootstrap-affix.js"></script>
<script>
    _386 = { 
        fastLoad: false ,
        onePass: false , 
        speedFactor: 1 
    };

    
    function ThisYear() {
        document.getElementById('thisyear').innerHTML = new Date().getFullYear();
    };
</script>
</html>
